package com.gl.emms.whale.ui;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.nio.mutual.ServerMessage;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.bean.User;
import com.gl.emms.whale.tranbean.TranObject;
import com.gl.emms.whale.tranbean.TranObjectType;
import com.gl.emms.whale.util.DialogFactory;
import com.gl.emms.whale.util.MD5;
import com.gl.emms.whale.util.RoomViewEntity;
import com.gl.emms.whale.util.SharePreferenceUtil;

/**注册界面类
 * @author ganlinux
 *
 */
public class RegisterActivity extends WhaleMonitorActivity implements OnClickListener {

	private Button mBtnRegister;
	private Button mRegBack;
	private EditText mNameEt, mPasswdEt, mPasswdEt2, mPhoneNumEt,mRoomNumEt,mCardNumEt,reg_weixinnum;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_register);
		initViews();

	}

	public void initViews() {
		mBtnRegister = (Button) findViewById(R.id.register_btn);
		mRegBack = (Button) findViewById(R.id.reg_back_btn);
		mBtnRegister.setOnClickListener(this);
		mRegBack.setOnClickListener(this);

		mNameEt = (EditText) findViewById(R.id.reg_name);
		mPasswdEt = (EditText) findViewById(R.id.reg_password);
		mPasswdEt2 = (EditText) findViewById(R.id.reg_password2);
		mPhoneNumEt = (EditText) findViewById(R.id.reg_phonenum);
		//取得启动该Activity的Intent对象
        Intent intent =getIntent();
        /*取出Intent中附加的数据*/
        String phone = intent.getStringExtra("phone");
		mPhoneNumEt.setText(phone);
		mRoomNumEt = (EditText) findViewById(R.id.reg_roomnum);
		mCardNumEt = (EditText) findViewById(R.id.reg_cardnum);
		reg_weixinnum = (EditText) findViewById(R.id.reg_cardnum);

	}
	@Override
	public void onBackPressed() {// 捕获返回键
		toast(RegisterActivity.this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.register_btn:
			showProgressDialog(getString(R.string.register_progresstip), getString(R.string.register_progresstext));
			estimate();
			break;
		case R.id.reg_back_btn:
			toast(RegisterActivity.this);
			break;
		default:
			break;
		}
	}

	private void toast(Context context) {
		new AlertDialog.Builder(context).setTitle(getString(R.string.register_toasttitle))
		.setMessage(getString(R.string.register_toasttext))
		.setPositiveButton(getString(R.string.register_toastok), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		}).setNegativeButton(getString(R.string.register_toastcancel), null).create().show();
	}
	@Override
	public void onMessageReceived(ServerMessage arg0) {
		super.onMessageReceived(arg0);


	}
	@Override
	public void onReplyReceived(final ReplyBody reply) {
		super.onReplyReceived(reply);
		if (reply.getType().equals(Constant.TYPE_REGISTER_RESPONSE)) {
			hideProgressDialog();
			if(reply.get(Constant.CODE_REGISTER).equals(Constant.REGISTER_SUCCESS))
			{
				SharePreferenceUtil util = new SharePreferenceUtil(this, Constant.SAVE_USERINFO);
				util.setUserName(mNameEt.getText().toString());
				showToask(getString(R.string.register_success));
				finish();
			}else
			{
				String result = "";
				if (reply.get(Constant.CODE_REGISTER).equals(Constant.REGISTER_USERNAMEREPEAT)) {
					result = getString(R.string.register_namerepeat);
				}else if(reply.get(Constant.CODE_REGISTER).equals(Constant.REGISTER_ROOMREPEAT)){
					result = getString(R.string.register_roomrepeat);
				}else if(reply.get(Constant.CODE_REGISTER).equals(Constant.REGISTER_CARDWRONG)){
					result = getString(R.string.register_cardwrong);
				}else if(reply.get(Constant.CODE_REGISTER).equals(Constant.REGISTER_CARDWRONG)){
					result = getString(R.string.register_cardwrong);
				}else{
					result = getString(R.string.register_unknown);
				}
				DialogFactory.ToastDialog(RegisterActivity.this, getString(R.string.register_fail),
						result);
			}

		}

	}
	private void estimate() {
		final String name = mNameEt.getText().toString();
		final String passwd = mPasswdEt.getText().toString();
		final String passwd2 = mPasswdEt2.getText().toString();
		final String phoneNum = mPhoneNumEt.getText().toString();
		final String roomNum = mRoomNumEt.getText().toString();
		final String cardNum = mCardNumEt.getText().toString();
		final String weixinNum = reg_weixinnum.getText().toString();
		if (name.equals("") || passwd.equals("")
				|| passwd2.equals("")|| phoneNum.equals("")|| roomNum.equals("")|| cardNum.equals("")) {
			hideProgressDialog();
			DialogFactory.ToastDialog(RegisterActivity.this, getString(R.string.register_toasttitle),
					getString(R.string.register_toastnotempty));
		} else {

			if (passwd.equals(passwd2)) {
				new AsyncTask<Void, Void, Void>() {
					List<User> list;

					protected Void doInBackground(Void... params) {
						// 提交注册信息
						String password = MD5.getMD5(passwd);
						String id = MyApplication.getUserData().getIdIP();
						EMMSPushManager.requestRegister(RegisterActivity.this,id,name,password,phoneNum,roomNum,cardNum,weixinNum);						
						return null;
					}

					@Override
					protected void onPostExecute(Void result) {
						//myExAdapter.notifyDataSetChanged();
						//Toast.makeText(TabActivity.this, "刷新成功", 0).show();
					}

				}.execute();




			} else {
				hideProgressDialog();
				DialogFactory.ToastDialog(RegisterActivity.this, getString(R.string.register_toasttitle),
						getString(R.string.register_toastpasswordwrong));
			}
		}

	}
}
